package com.oil.petro.eshop.controller;

import com.oil.petro.eshop.bean.BasicBean;
import com.oil.petro.eshop.bean.UserBean;
import com.oil.petro.eshop.entity.Account;
import com.oil.petro.eshop.entity.Order;
import com.oil.petro.eshop.entity.Person;
import com.oil.petro.eshop.entity.Product;
import com.oil.petro.eshop.service.CategoryService;
import com.oil.petro.eshop.service.ManufacturerService;
import com.oil.petro.eshop.service.OrderService;
import com.oil.petro.eshop.service.ProductService;
import com.oil.petro.eshop.service.UserAccountService;
import com.oil.petro.eshop.utils.NavigationBar;
import com.oil.petro.eshop.utils.NavigationMenu;
import com.oil.petro.eshop.utils.ProductHelper;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CartController {

    ProductHelper productHelper = new ProductHelper();
    @Autowired
    CategoryService categoryService;

    @Autowired
    ManufacturerService manufacturerService;

    @Autowired
    ProductService productService;

    @Autowired
    UserAccountService accountService;

    @Autowired
    OrderService orderService;

    @RequestMapping(value = "/cart", method = RequestMethod.GET)
    public ModelAndView showCartStep1(@RequestParam("step") int step,
            @RequestParam(value = "delete", required = false) Integer orderId,
            HttpServletRequest request) {

        Map<String, Object> model = new HashMap<String, Object>();
        //BasicBean basicBean = new BasicBean();
        NavigationBar bar = new NavigationBar();
        NavigationMenu menu = new NavigationMenu();

        Account account = accountService.getAccountByLogin(request.getUserPrincipal().getName());

        Person person = accountService.getPerson(account.getPersonId());
        UserBean userBean = new UserBean(person);
        userBean.setUser(account.getLogin());
        //   ProductHelper productHelper = new ProductHelper();
        bar.getNavigationBarDataModel(model, userBean, accountService);
        menu.getNavigationMenuDataModel(model, categoryService, manufacturerService);
        if (userBean.getOrderCounter() != 0) {

            if (orderId != null) {

                System.out.println("OrderId" + orderId);
                orderService.removeOrder(orderId);

            }

            if (step == 1) {

                model.put("sum", productHelper.getDisplayedPrice(orderService.getTotalCost(account.getId()))
                );
                model.put("itemList", orderService.getOrders(account.getId()));
                bar.getNavigationBarDataModel(model, userBean, accountService);
            } else if (step == 2) {
                return new ModelAndView("cart/cartStep2", model);

            } else if (step == 4) {

                orderService.removeOrders(account.getId());

                bar.getNavigationBarDataModel(model, userBean, accountService);

                return new ModelAndView("cart/cartStep4", model);

            }

        }

        return new ModelAndView("cart/cartStep1", model);

    }

    @RequestMapping(value = "/cart", method = RequestMethod.POST)
    public ModelAndView onSubmitCartStep1(@ModelAttribute("commandBean") UserBean userBean, @RequestParam("step") int step, HttpServletRequest request) {

        NavigationBar bar = new NavigationBar();
        NavigationMenu menu = new NavigationMenu();
        Account account = accountService.getAccountByLogin(request.getUserPrincipal().getName());

        Person person = accountService.getPerson(account.getPersonId());

        UserBean bean = new UserBean(person);
        bean.setComment(userBean.getComment());
        bean.setUser(account.getLogin());
        Map<String, Object> model = new HashMap<String, Object>();

        model.put("sum", productHelper.getDisplayedPrice(orderService.getTotalCost(account.getId())));
        model.put("itemList", orderService.getOrders(account.getId()));
        //UserBean userBean = new UserBean();
        bar.getNavigationBarDataModel(model, bean, accountService);
        menu.getNavigationMenuDataModel(model, categoryService, manufacturerService);

        return new ModelAndView("cart/cartStep3", model);

    }

}
